<template>
	<view>
		<!-- 列表框 -->
		<tabs @setSwipterData="setSwipterData" :tabList="tabItems"></tabs>
		<!-- 列表 -->
		<view v-for="(item, index) in contents" :key="index" v-if="contents.length > 0" :hidden="item == null" class="list"
		 @tap="goDetail" :data-index="index" :data-id="item.id">
			<view class="list-title">
				{{item.typeOfWork == 1 ? item.title + '-' + '招工' : item.title + '-' + "找活"}}
				<text class="list-status">{{item.status==0?"审核中":item.status==1?"审核通过":item.status==2?"招工完成":""}}</text>
			</view>
			<view class="list-claim">详情：{{item.claim}}</view>
			<view class="list-date">发布时间：{{item.gmtModified}}</view>
		</view>
		<view v-if="contents.length <= 0">
			<view class="list">
				<view class="list-title">暂时没有数据</view>
				<view class="list-company">详情：None</view>
				<view class="list-date">发布时间：None</view>
			</view>
		</view>
	</view>
</template>

<script>
	// pages/ucenter/mine/recruit.js
	const app = getApp();
	import tabs from "../../../components/tabs/tabs";

	export default {
		data() {
			return {
				tabItems: [
					["全部", "", true],
					["正在招/正在找", "", false],
					["已招满/已找到", "", false]
				],
				userId: "",
				contents: [],
				actionSheetChange: true
			};
		},

		components: {
			tabs
		},
		props: {},
		onLoad: function(options) {
			let _this = this; // 获取用户Id


			let openId = uni.getStorageSync('openid');

			if (openId != null && openId != '' && openId != undefined) {
				uni.showLoading({
					title: '加载中' // 内容
				});
				uni.request({
					url: `${app.globalData.baseUrl}/user/selectIsUser/${openId}`,
					// 目标服务器url
					success: res => {
						let userId = res.data.dataMap.data.id;

						_this.setData({
							userId
						});

						uni.request({
							url: `${app.globalData.baseUrl}/user/userReleaseWorkListById/${_this.userId}`,
							// 目标服务器url
							success: res => {
								let contents = res.data.dataMap.data;
								contents = contents.filter(content => content != null);
								_this.setData({
									contents
								});
							}
						});
					},

					complete() {
						uni.hideLoading();
					}

				});
			} else {
				uni.navigateBack({
					complete: res => {
						uni.showToast({
							title: '请登录',
							icon: "none"
						});
					}
				});
			}
		},
		onShow: function() {},
		onReachBottom: function() {},
		methods: {
			setSwipterData(e) {
				const current = e.detail;

				let _this = this;

				if (current == 0) {
					uni.showLoading({
						title: '加载中'
					});
					uni.request({
						url: `${app.globalData.baseUrl}/release-work/work/mine/${_this.userId}?status=0`,
						// 目标服务器url
						success: res => {
							let contents = res.data.dataMap.data;
							contents = contents.filter(content => content != null);
							_this.setData({
								contents
							});
						},

						complete() {
							uni.hideLoading();
						}

					});
				}

				if (current == 1) {
					uni.showLoading({
						title: '加载中'
					});
					uni.request({
						url: `${app.globalData.baseUrl}/release-work/work/mine/${_this.userId}?status=1`,
						// 目标服务器url
						success: res => {
							let contents = res.data.dataMap.data;
							contents = contents.filter(content => content != null);
							_this.setData({
								contents
							});
						},

						fail(res) {
							_this.setData({
								contents: []
							});
						},

						complete() {
							uni.hideLoading();
						}

					});
				}

				if (current == 2) {
					uni.showLoading({
						title: '加载中' // 内容

					});
					uni.request({
						url: `${app.globalData.baseUrl}/release-work/work/mine/${_this.userId}?status=2`,
						// 目标服务器url
						success: res => {
							let contents = res.data.dataMap.data;
							contents = contents.filter(content => content != null);
							_this.setData({
								contents
							});
						},

						fail(res) {
							_this.setData({
								contents: []
							});
						},

						complete() {
							uni.hideLoading();
						}

					});
				}

			},

			goDetail: function(e) {
				let id = e.currentTarget.dataset.id;
				let index = e.currentTarget.dataset.index;

				let _this = this;

				const itemList = ["查看详情", "已招满", "删除"];
				uni.showActionSheet({
					itemList,

					success(res) {
						switch (res.tapIndex) {
							case 0:
								uni.navigateTo({
									url: `/pages/common/detail/detail?id=${id}` // 指定页面的url

								});
								break;

							case 1:
								let content = _this.contents[index];

								if (content.status === 1) {
									content.status = 2;
								} else {
									uni.showToast({
										title: '状态不可修改',
										// 内容
										icon: "none"
									});
									return false;
								}

								uni.request({
									url: `${app.globalData.baseUrl}/release-work/work`,
									// 目标服务器url
									method: "put",
									data: content,
									success: res => {
										uni.navigateTo({
											url: '/pages/ucenter/mine/mine' // 指定页面的url

										});
										uni.showToast({
											title: '修改成功'
										});
									}
								});
								break;

							case 2:
								uni.showModal({
									title: "提示",
									content: "确定删除吗",

									success(res) {
										if (res.confirm) {
											uni.request({
												url: `${app.globalData.baseUrl}/release-work/work/${id}`,
												// 目标服务器url
												method: "delete",
												success: res => {
													uni.navigateTo({
														url: '/pages/ucenter/mine/mine' // 指定页面的url

													});
													uni.showToast({
														title: '删除成功'
													});
												}
											});
										} else if (res.cancel) {} else {}
									},

									fail(res) {
										console.log(`showModal调用失败`);
									}

								});
								break;
						}
					},

					fail(res) {}

				});
			},

			onShareTimeline() {}

		}
	};
</script>
<style>
	/* pages/ucenter/mine/recruit.ttss */
	.list {
		width: 95%;
		height: 200rpx;
		margin: 2.5%;
		border-bottom: solid 1px #e6e6ee;
		background: #fff;
		padding: 5rpx 10rpx;
	}

	.list-title {
		font-size: 40rpx;
		margin-top: 20rpx;
	}

	.list-date {
		margin-top: 10rpx;
	}

	.list-claim {
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 1;
		-webkit-box-orient: vertical;
	}

	.list-status {
		float: right;
		font-size: small;
		color: red;
	}

	page {
		background: #efeff5;
	}
</style>
